package com.example.myapp.activities.main_activity.sectionFragments;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.example.myapp.R;
import com.example.myapp.activities.add_bill.AddBillActivity;
import com.example.myapp.adapters.StableArrayAdapter;
import com.example.myapp.bill.Bill;
import com.example.myapp.database.BillDao;

import java.util.List;

/**
 * A dummy fragment representing a section of the app, but that simply
 * displays dummy text.
 */
public class DummySectionFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemClickListener {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    private BillDao billDao;

    private StableArrayAdapter adapter;

    private View rootView;

    private EditText dateET;

    private EditText amountET;

    private ListView listView;

    private Button dodajBtn;

    private Button usunBtn;

    private Button oplacBtn;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_main_dummy,
                container, false);

        initializeComponents();
        setOnClickListeners();
        setAdapter();

        return rootView;
    }

    private void initializeComponents() {
        billDao = new BillDao(getActivity());

        listView = (ListView) rootView.findViewById(R.id.listView1);
        dodajBtn = (Button) rootView.findViewById(R.id.dodajBtn);
        usunBtn = (Button) rootView.findViewById(R.id.usunBtn);
        oplacBtn = (Button) rootView.findViewById(R.id.oplacBtn);

        dateET = (EditText) rootView.findViewById(R.id.dataEditText);
        amountET = (EditText) rootView.findViewById(R.id.kwotaEditText);

    }

    private void setOnClickListeners() {
        dodajBtn.setOnClickListener(this);
        usunBtn.setOnClickListener(this);
        oplacBtn.setOnClickListener(this);
        listView.setOnItemClickListener(this);
    }

    private void setAdapter() {
        List<Bill> allListaBills = billDao.getAllListaBills();
        adapter = new StableArrayAdapter(rootView.getContext(), allListaBills);
        listView.setAdapter(adapter);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dodajBtn:
                adapter.unselectAll();
                dateET.setText("");
                amountET.setText("");
                Intent intent = new Intent(getActivity().getApplicationContext(), AddBillActivity.class);
                startActivity(intent);
                break;
            case R.id.usunBtn:
                if (adapter.isAnySelected()) {
                    billDao.delete(adapter.getSelectedItem());
                    adapter.removeItem(adapter.getSelectedItemPos());
                    dateET.setText("");
                    amountET.setText("");
                    Toast.makeText(getActivity().getApplicationContext(), "Usunięto rachunek", Toast.LENGTH_LONG).show();
                } else
                    showNoBillSelectedDialog("usunąć");
                break;
            case R.id.oplacBtn:
                if (adapter.isAnySelected()) {
                    listView.setAdapter(adapter);
                    adapter.setItemPaid(adapter.getSelectedItemPos());
                    billDao.paidBill(adapter.getSelectedItem());
                } else {
                    showNoBillSelectedDialog("opłacić");
                }
                break;
        }
    }


    private void showNoBillSelectedDialog(String label) {
        AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
        alertDialog.setTitle("Informacja");
        alertDialog.setMessage("Nie wybrałeś rachunku którego chcesz " + label);
        alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        alertDialog.show();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId()) {
            case R.id.listView1:
                adapter.setSelected(position);
                dateET.setText(adapter.getSelectedItem().getDate());
                amountET.setText(String.valueOf(adapter.getSelectedItem().getAmount() + " zł"));
                break;
        }
    }
}